﻿CREATE PROCEDURE [acms].[UserPostComment_Put]
	@Id int,
	@UserId int,
	@PostId int,
	@Title varchar(500), 
	@Comment varchar(2000), 
	@IPAddress varchar(16),
	@Reviewed bit, 
	@Reported bit, 
	@Suspended bit, 
	@Created DateTime, 
	@ConcurencyId rowversion,
	@IdOut int output,
	@UserNameOut varchar(50) output, 
	@ConcurencyIdOut rowversion output
AS
	if exists (select null From UserPostComment_View with (nolock) where Id = @Id) begin
		update UserPostComment
		set
			Title = @Title, 
			Comment = @Comment, 
			Reviewed = @Reviewed, 
			Reported = @Reported, 
			Suspended = @Suspended
		where Id = @Id
			and ConcurencyId = @ConcurencyId
		
		if @@rowcount = 0
			return -100
		else
			set @IdOut = @Id
	end
	else if(@Id is null or @Id = 0) begin
		insert into UserPostComment
			(UserId, PostId, Title, Comment, IPAddress, Reviewed, Reported, Suspended, Created)
		values
			(@UserId, @PostId, @Title, @Comment, @IPAddress ,@Reviewed, @Reported, @Suspended, @Created)
		set @IdOut = @@Identity
	end
	else begin
		return -200
	end
	
	select @UserNameOut = UserName, @ConcurencyIdOut = ConcurencyId from UserPostComment_View with (nolock) where Id = @IdOut
RETURN 0;